<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/metadata/productCategory/productCategoryList' }">商品分类列表</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form label-width="90px" size="large" disabled :model="editForm">

          <el-row>
            <el-col :span="12">
              <el-form-item label="上级" prop="parentId">
                <autumn-tree-select :data="treeCategorys" v-model="editForm.parentId" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码" prop="code">
                <el-input v-model="editForm.code" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input type="number" v-model="editForm.sort" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>

        <div class="form-foot-button">
          <el-button @click="cancel">返回</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { $convertTree } from 'src/util/tools'
import { listProductCategory, viewProductCategory } from 'src/api/metadata/productCategory'

// 查询商品分类
const treeCategorys = ref<Array<any>>([])
listProductCategory().then(({ data }) => {
  treeCategorys.value = $convertTree(data)
})

// 查看商品分类
const editForm = reactive({
  id: '',
  parentId: '',
  code: '',
  name: '',
  sort: 1,
  remark: ''
})

const route = useRoute()
nextTick(() => {
  if (route.query.id) {
    const id = route.query.id as string
    viewProductCategory(id).then(({ data }) => {
      Object.assign(editForm, data)
    })
  }
})

// 返回
const router = useRouter()
function cancel() {
  router.push('/metadata/productCategory/productCategoryList')
}

</script>

<style lang="scss" scoped></style>
